<?php
namespace Modules\Basic\Http\Controllers;

use App\Http\Controllers\Controller;
use App\services\Wechatservice;
use Illuminate\Http\Request;
use Modules\Basic\Entities\BaseReply;
use Modules\Basic\Http\Requests\BaseReplyRequest;
use Modules\Wx\Entities\WxKeyword;
use Modules\Wx\Entities\WxRole;

class BaseReplyController extends Controller
{
    //显示列表
    public function index()
    {
        $data = BaseReply::paginate(10);
        return view('basic::base_reply.index', compact('data'));
    }

    //创建视图
    public function create(BaseReply $base_reply,Wechatservice $wechatservice)
    {
//        dd(123);
//        加载公共视图
        $view = $wechatservice->keyshitu();
        return view('basic::base_reply.create',compact('base_reply','view'));

    }

    //保存数据
    public function store(BaseReplyRequest $request,BaseReply $base_reply,Wechatservice $wechatservice)
    {

//        dd($request->all());
//        $post = json_decode($request->input('rule'),true);
////        dd($post);
////        dd($request->all());
////        存规则表
//
////        开始一个事物
//        \DB::beginTransaction();
//        $rule = WxRole::create([
//           'name'=>$post['name'],
//            'module'=>\HDModule::currentModule(),
//        ]);
////        存关键词表
//        foreach ($post['keywords'] as $v){
//            WxKeyword::create([
//               'key'=>$v['key'],
//                'role_id'=>$rule['id']
//            ]);
//        }
//        存基本回复表
//        $haha = json_decode($request->input('content'),true);
//        dd($request->input('content'));
//        foreach ($haha as $v){
//            BaseReply::create([
//                'content'=>$v['content'],
//                'role_id'=>$rule['id']
//            ]);
//        }

        $rule = $wechatservice->saverule();
//        dd($rule);
//        把内容杜普存到一块去，才不能同时出现两条数据
        BaseReply::create([
           'content'=>$request->input('content'),
            'role_id'=>$rule['id']
        ]);
//        \DB::commit();
//        结束这个事物
//        $base_reply->fill($request->all());
//        $base_reply->save();
        return redirect('/basic/base_reply')->with('success', '保存成功');
    }

    //显示记录
    public function show(BaseReply $field)
    {
        return view('basic::base_reply.show', compact('field'));
    }

    //编辑视图
    public function edit(BaseReply $base_reply,Wechatservice $wechatservice)
    {
//        dd(123);
        $view = $wechatservice->keyshitu($base_reply['role_id']);
        return view('basic::base_reply.edit', compact('base_reply','view'));
    }

    //更新数据
    public function update(BaseReplyRequest $request, BaseReply $base_reply,Wechatservice $wechatservice)
    {

//        意思就是把关键词表和规则表的添加都放到外面去，回复内容放到里边

        $rule = $wechatservice->saverule($base_reply['role_id']);
        //将回复内容数据存入BaseReply表
//        dd($rule);
        $base_reply->content = $request->input('content');
        $base_reply->role_id = $rule['id'];
        $base_reply->save();
//        dd(123);可以进来,下一步:编辑添加进行合并
//        $base_reply->update($request->all());

        return redirect('/basic/base_reply')->with('success','更新成功');
    }

    //删除模型
    public function destroy(BaseReply $base_reply)
    {
        WxRole::destroy($base_reply['role_id']);
        $base_reply->delete();
        return redirect('basic/base_reply')->with('success','删除成功');
    }
}